package com.aplink.generic.pojo;

import android.text.TextUtils;

public class UpdateInfoRecord {
	private boolean isDropColumn;
	private boolean isDropTable;
	private String mColumn;
	private String mColumnType;
	private String mDefaultValue;
	private String mTableName;
	private int mVersion;

	public String getColumn() {
		return mColumn;
	}

	public String getColumnType() {
		return mColumnType;
	}

	public String getDefaultValue() {
		return mDefaultValue;
	}

	public String getDropBakTableSql() {
		return String.format("DROP TABLE IF EXISTS %s_bak;", mTableName);
	}

	public String getDropColumnSql() {
		return String.format(", %s %s", mColumn, mColumnType);
	}

	public String getDropTableSql() {
		return String.format("DROP TABLE IF EXISTS %s;", mTableName);
	}

	public String getInsertSql(String createSql) {
		String insertSql = createSql.replaceAll("CREATE TABLE", "INSERT INTO")
		        .replaceAll("Char", "").replaceAll("Integer", "")
		        .replaceAll("TEXT", "").replaceAll("Double", "")
		        .replaceAll("Long", "").replaceAll("PRIMARY KEY", "")
		        .replaceAll("AUTOINCREMENT", "").replaceAll("NOT NULL", "")
		        .replaceAll(String.format(", %s", mColumn), "")
		        .replaceAll(";", "");
		String selectSql = insertSql
		        .replaceAll(String.format("INSERT INTO %s", mTableName),
		                " SELECT ").replaceAll("\\(", "").replaceAll("\\)", "")
		        .concat(String.format(" FROM %s_bak", mTableName));
		return insertSql.concat(selectSql).concat(";");
	}

	public String getRenameSql() {
		return String.format("ALTER TABLE %s RENAME TO %s_bak;", mTableName,
		        mTableName);
	}

	public String getTableName() {
		return mTableName;
	}

	public String getUpdateSql() {
		if (TextUtils.isEmpty(mDefaultValue)) {
			return String.format("ALTER TABLE %s ADD COLUMN %s %s;",
			        mTableName, mColumn, mColumnType);
		}
		return String.format("ALTER TABLE %s ADD COLUMN %s %s DEFAULT %s;",
		        mTableName, mColumn, mColumnType, mDefaultValue);
	}

	public int getVersion() {
		return mVersion;
	}

	public boolean isDropColumn() {
		return isDropColumn;
	}

	public boolean isDropTable() {
		return isDropTable;
	}

	public void setColumn(String column) {
		mColumn = column;
	}

	public void setColumnType(String columnType) {
		mColumnType = columnType;
	}

	public void setDefaultValue(String defaultValue) {
		mDefaultValue = defaultValue;
	}

	public void setDropColumn(boolean isDropColumn) {
		this.isDropColumn = isDropColumn;
	}

	public void setDropTable(boolean isDropTable) {
		this.isDropTable = isDropTable;
	}

	public void setTableName(String tableName) {
		mTableName = tableName;
	}

	public void setVersion(int version) {
		mVersion = version;
	}

}
